﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YBApproval.Model;
using YBApproval.IDAL;
using YBApproval.BLL;
using YBApproval.UserControls;

namespace YBApproval.Pages
{
    public partial class View : System.Web.UI.Page
    {
        private string _recordId = "";          // 247C0C90-F66A-459C-B6F4-8D19161BAD2C
        private Instance _inst = null;
        private List<InstanceApproval> _instApprovalList = new List<InstanceApproval>();

        private int _instApprovalId;
        private Guid _instId;
        private int _processId;

        private IInstance _instDAL = DALFactory.GetDAL<IInstance>();
        private INode _nodeDAL = DALFactory.GetDAL<INode>();
        private IInstanceApproval _instApprovalDAL = DALFactory.GetDAL<IInstanceApproval>();
        private IForm _formDAL = DALFactory.GetDAL<IForm>();

        protected void Page_Load(object sender, EventArgs e)
        {
            //added by Natalia
            //在主页标题栏上设置标题。
            Master.SetContentTitle("查看记录");


            // url传递 recordId 到本页面
            _recordId = Request.Params["RecordId"];
            this.RequestViewer.RecordId = _recordId;

            LoadData();

            AppendPreviousApprovals();

            if (!IsPostBack)
            {

            }
            else
            {
                if (ViewState["_instId"] != null)
                {
                    _instId = new Guid(ViewState["_instId"].ToString());
                }

                if (ViewState["_instApprovalId"] != null)
                {
                    _instApprovalId = Convert.ToInt32(ViewState["_instApprovalId"]);
                }
            }
        }

        private void LoadData()
        {
            _inst = _instDAL.GetInstance(_recordId);
            _instApprovalList = _instApprovalDAL.GetInstanceApprovals(_recordId);
            _processId = _formDAL.GetProcessId(_inst.FormId);
        }



        /// <summary>
        /// 动态生成 approval 的页面项目
        /// </summary>
        private void AppendPreviousApprovals()
        {
            foreach (InstanceApproval ia in _instApprovalList)
            {
                // this node has been approved or denied
                //if (ia.Step != _inst.Step)
                //{
                //    UCApprovalView ucAV = (UCApprovalView)Page.LoadControl("/UserControls/UCApprovalView.ascx");
                //    ucAV.ApprovalIns = ia;
                //    Panel1.Controls.Add(ucAV);
                //}

                UCApprovalView ucAV = (UCApprovalView)Page.LoadControl("/UserControls/UCApprovalView.ascx");
                ucAV.ApprovalIns = ia;
                Panel1.Controls.Add(ucAV);
            }
        }




        #region 事件
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            // 将属性保存到ViewState中
            ViewState.Add("_instId", _instId);
            ViewState.Add("_instApprovalId", _instApprovalId);
        }
        #endregion

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Default.aspx");
        }
    }
}